const { Sequelize, Model, STRING } = require('sequelize')
const { sequelize } = require('../../core/db')
STRING(12)
class Comment extends Model {

    static async addComment(bookID, content) {
        
        // 点赞加一
        // 添加相同的评论   和加一相同

        // 相同评论是否存在
        const comment = await Comment.findOne({
            where: {
                book_id: bookID,
                content
            }
        })
        if (!comment) {
            // 不存在 出入content
            return await Comment.create({
                book_id: bookID,
                content,
                nums: 1
            })
        } else {
            // 存在 加一 increment 字段值自增
            return await comment.increment('nums', {
                by: 1
            })
        }

    }

      // 获取短评
      static async getComment(bookID){
          const comments = await Comment.findAll({
              where:{
                  book_id:bookID
              }
          })
          return comments
      }
}

Comment.init({
    content: Sequelize.STRING(12),
    nums: {
        type: Sequelize.INTEGER,
        defaultValue: 0
    },
    book_id: Sequelize.INTEGER
}, {
    sequelize,
    tableName: 'comment'
})

module.exports = {
    Comment
}